package com.woke.method;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.Log;

import java.lang.ref.WeakReference;

/**
 * 用户Viewpager 自动滑动
 */
public class ImageHandler<T> extends Handler implements ViewPager.OnPageChangeListener {

	/**
	 * 更新显示 View
	 */
	public static final int MSG_UPDATE_IMAGER = 1;
	/**
	 * 延迟时间用
	 */
	public static final int MSG_DELAY = 3000;
	/**
	 * 暂停轮播
	 */
	protected static final int MSG_KEEP_SILENT = 2;
	/**
	 * 恢复轮播
	 */
	protected static final int MSG_BREAK_SILENT = 3;
	/**
	 * 记录最新的页号,当用户手动滑动时需要记录新的页号,否则会使页面轮播出现错误
	 */
	protected static final int MSG_PAGE_CHANGE = 4;
	/**
	 * JAVA WeakReference 弱引用 ,用于GC 资源回收
	 */
	protected WeakReference<T> mWeakReference;
	/**
	 * 当Activity 或者 Fragment 处于不可见或者销毁时,暂停轮播
	 */
	private boolean mIsPause = false;
	private ViewPager mViewPager;
	private int mCurrentItem = 0;
	private Context mContext;

	public ImageHandler(Context context) {
		this.mContext = context;
		this.mWeakReference = new WeakReference<T> ((T) context);
	}

	public void setViewPage(ViewPager viewpage, int initPosition) {
		setViewPage (viewpage);
		setCurrentItem (initPosition);
	}

	public void setViewPage(ViewPager viewpage) {
		if (mViewPager == viewpage) {
			return;
		}
		if (mViewPager != null) {
			mViewPager.removeOnPageChangeListener (this);
		}
		if (viewpage.getAdapter () == null) {
			throw new IllegalStateException ("ViewPager does not hava adapger instance.");
		}
		mViewPager = viewpage;
		mViewPager.addOnPageChangeListener (this);
		mCurrentItem = 0;
	}

	public void setCurrentItem(int item) {
		if (mViewPager == null) {
			throw new IllegalStateException ("ViewPager has not been bound.");
		}
		mViewPager.setCurrentItem (item);
		mCurrentItem = item;
	}

	@Override
	public void handleMessage(Message msg) {
		super.handleMessage (msg);

		Log.e ("ImageHandler", "receive  messager " + msg.what);
		T t = mWeakReference.get ();
		if (t == null) return;

		if (mIsPause) {
			Log.e ("ImageHandler", "isPause==" + true);
			return;
		}

		if (mViewPager == null || mViewPager.getAdapter () == null) return;

		//检查消息队列并移除未发送的消息,避免在复杂情况下 消息出现重复等问题
		if (this.hasMessages (MSG_UPDATE_IMAGER)) {
			removeMessages (MSG_UPDATE_IMAGER);
		}

		switch (msg.what) {
			case MSG_UPDATE_IMAGER:
				//更新V
				mCurrentItem++;
				if (mCurrentItem >= mViewPager.getAdapter ().getCount ()) {
					mCurrentItem = 0;
				}
				mViewPager.setCurrentItem (mCurrentItem);
				Log.e ("ImageHandler", "mCurrentItem==" + mCurrentItem);
				this.sendEmptyMessageDelayed (MSG_UPDATE_IMAGER, MSG_DELAY);
				break;
			case MSG_KEEP_SILENT:
				//暂停轮播  暂不做处理
				break;
			case MSG_BREAK_SILENT:
				//恢复轮播
				this.sendEmptyMessageDelayed (MSG_UPDATE_IMAGER, MSG_DELAY);
				break;
			case MSG_PAGE_CHANGE:
				//更新当前页号
				mCurrentItem = msg.arg1;
				break;
			default:
				break;
		}


	}

	/**
	 * 当Activity 或者 Fragment 处于不可见或者销毁时,暂停轮播
	 *
	 * @param isPause
	 */
	public void setIsPause(boolean isPause) {
		mIsPause = isPause;
	}

	/**
	 * Viewpager 监听事件
	 */

	@Override
	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

	}

	@Override
	public void onPageSelected(int position) {
		Message msg = Message.obtain ();
		msg.what = MSG_PAGE_CHANGE;
		msg.arg1 = position;
		this.sendMessage (msg);
	}

	/**
	 * @param state ViewPager.SCROLL_STATE_DRAGGING   1,正在滑动
	 *              ViewPager.SCROLL_STATE_SETTLING    2,滑动完毕
	 *              ViewPager.SCROLL_STATE_IDLE       0,默认什么多没做
	 */
	@Override
	public void onPageScrollStateChanged(int state) {
		switch (state) {
			case ViewPager.SCROLL_STATE_IDLE:
				this.sendEmptyMessageDelayed (MSG_UPDATE_IMAGER, MSG_DELAY);
				break;
			case ViewPager.SCROLL_STATE_DRAGGING:
				this.sendEmptyMessage (MSG_KEEP_SILENT);
				break;
			case ViewPager.SCROLL_STATE_SETTLING:
//                  this.sendEmptyMessage(MSG_BREAK_SILENT);
				break;

			default:
				break;
		}

	}
}
